package com.videostar.funforfollowers.utils

import android.content.Context
import android.util.Log
import com.appsflyer.AFInAppEventParameterName
import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib

/**
 * @author Afra55
 * @date 2020/8/2
 * A smile is the best business card.
 */
object AppsflyerUtils {
    val LOG_TAG = "AppsflyerUtils"
    var context:Context? = null
    fun init(applicationContext: Context, devKey: String, debug: Boolean) {
        context = applicationContext
        val conversionDataListener = object : AppsFlyerConversionListener {
            override fun onConversionDataSuccess(data: MutableMap<String, Any>?) {
                if (debug) {
                    data?.let { cvData ->
                        cvData.map {
                            Log.i(LOG_TAG, "conversion_attribute:  ${it.key} = ${it.value}")
                        }
                    }
                }
            }

            override fun onConversionDataFail(error: String?) {
                if (debug)
                    Log.e(LOG_TAG, "error onAttributionFailure :  $error")
            }

            override fun onAppOpenAttribution(data: MutableMap<String, String>?) {
                if (debug)
                    data?.map {
                        Log.d(LOG_TAG, "onAppOpen_attribute: ${it.key} = ${it.value}")
                    }
            }

            override fun onAttributionFailure(error: String?) {
                if (debug)
                    Log.e(LOG_TAG, "error onAttributionFailure :  $error")
            }
        }

        AppsFlyerLib.getInstance().init(devKey, conversionDataListener, applicationContext)
        if (debug) {

            AppsFlyerLib.getInstance().setDebugLog(debug)
        }
        AppsFlyerLib.getInstance().startTracking(applicationContext)
    }

    fun trackEvent(eventName:String, map:HashMap<String, Any>?) {
        AppsFlyerLib.getInstance().trackEvent(context, eventName, map)
    }

    fun track_app_launch() {
        trackEvent("app_launch", null)
    }
    fun track_core_showup() {
        trackEvent("core_showup", null)
    }
    fun track_core_button_click() {
        trackEvent("core_button_click", null)
    }
    fun track_core_download() {
        trackEvent("core_download", null)
    }
}